package com.chuyu.cy.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.chuyu.cy.contants.PageResult;
import com.chuyu.cy.contants.ResponseCodeEnum;
import com.chuyu.cy.contants.Result;
import com.chuyu.cy.entity.vo.MessageVO;
import com.chuyu.cy.service.ChatMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author: white-zhou
 * @date: 2024-03-26 21:09
 **/
@RestController
@RequestMapping("/message")
@Tag(name = "消息管理")
public class MessageController {

    @Resource
    private ChatMessageService chatMessageService;



    /**
     * 获取聊天记录
     * @param userId the sender
     * @param contactorId the contactor
     * @param pageNum the page number
     * @return the message list
     */
    @GetMapping("/list")
    @Operation(summary = "获取聊天记录")
    public Result<PageResult<MessageVO>> getMessageList(@RequestParam("userId") Integer userId,
                                                        @RequestParam("contactorId") Integer contactorId,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Result<PageResult<MessageVO>> result = new Result<>();

        if (userId == null || contactorId == null || pageNum == null) {
            return result.failure(ResponseCodeEnum.NO_PARAM);
        }

        PageResult<MessageVO> pageResult = chatMessageService.getMessageList(userId, contactorId, pageNum, 10);
        if (CollectionUtil.isEmpty(pageResult.getList())) {
            return result.failure(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return result.success(pageResult);
    }


    /**
     * 保存消息
     * @param messageVO the message
     * @return the result
     */
    @PostMapping("/save")
    @Operation(summary = "保存消息")
    public Result<MessageVO> saveMessage(@RequestBody MessageVO messageVO) {
        Result<MessageVO> result = new Result<>();

        if (messageVO == null) {
            return result.failure(ResponseCodeEnum.NO_PARAM);
        }

        MessageVO saved = chatMessageService.saveMessage(messageVO);
        if (saved == null) {
            return result.failure(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return result.success(saved);
    }
}
